//
//  ShellMilestoneCell.m
//  ShellWallert
//
//  Created by lvzhao on 2019/10/3.
//  Copyright © 2019 VV. All rights reserved.
//

#import "ShellMilestoneCell.h"
#import "ShellMilestoneModel.h"

@interface ShellMilestoneCell()


//历程
@property (nonatomic,strong) UILabel *milestoneLab;

@property (nonatomic,strong) CAShapeLayer *shapeLayer;
@end

@implementation ShellMilestoneCell

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.accessoryType  = UITableViewCellAccessoryNone;
        
        self.backgroundColor = UIColorHex(0x08233F);

        [self setupView];
    }
    return self;
}


#pragma mark - setupView
- (void)setupView{
    
    
    UIView *quanView = [[UIView alloc] init];
    quanView.bColor = UIColorHex(0x4AB2E7);
    quanView.bWidth = 2;
    quanView.cornerRadius = 5;
    [self.contentView addSubview:quanView];
    [quanView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.top.mas_equalTo(5);
        make.width.mas_equalTo(10);
        make.height.mas_equalTo(10);
    }];
    
    [self.contentView addSubview:self.milestoneLab];
    [self.milestoneLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(quanView.mas_top).mas_offset(-3);
        make.left.mas_equalTo(quanView.mas_right).mas_offset(20);
        make.right.mas_equalTo(0);
        make.height.mas_equalTo(35);
    }];
    
    UIImageView *lineView = [[UIImageView alloc] init];
    [self.contentView addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
      make.centerX.mas_equalTo(quanView.mas_centerX);
      make.top.mas_equalTo(quanView.mas_bottom).mas_offset(5);
      make.width.mas_equalTo(1);
      make.bottom.mas_equalTo(self.milestoneLab.mas_bottom).mas_offset(20);
    }];
      
     dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
      [self drawLineOfDashByCAShapeLayer:lineView lineLength:1 lineSpacing:3 lineColor:UIColorHex(0xB3BED9) lineDirection:NO];
    });
    
}


#pragma mark - actions

#pragma mark - custool

#pragma mark - get && set
- (void)setModelObject:(id)modelObject{
    ShellMilestoneModel *milestoneModel = modelObject;
    
    NSString *milestoneString = [NSString stringWithFormat:@"%@ %@",milestoneModel.time ?:@"",milestoneModel.desc ?:@""];
    NSMutableAttributedString *milestoneAttributedString = [NSAttributedString attributedString:milestoneString rangeTitle:milestoneModel.time leftFont:k_textFont(12) rightFont:k_textFont(12) leftColor:UIColorHex(0xFFFFFF) rightColor:UIColorHex(0x8D99B2)];
    self.milestoneLab.attributedText = milestoneAttributedString;
    
    
    [self.milestoneLab mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(milestoneModel.milestoneHeight);
    }];
}



//历程
- (UILabel *)milestoneLab{
    if(!_milestoneLab){
        _milestoneLab = [UILabel lz_createLabWithTextColor:UIColorHex(0xFFFFFF) font:k_textFont(12)];
        _milestoneLab.numberOfLines = 0;
    }
    return _milestoneLab;
}

/**
 *  通过 CAShapeLayer 方式绘制虚线
 *
 *  param lineView:       需要绘制成虚线的view
 *  param lineLength:     虚线的宽度
 *  param lineSpacing:    虚线的间距
 *  param lineColor:      虚线的颜色
 *  param lineDirection   虚线的方向  YES 为水平方向， NO 为垂直方向
 **/
- (CAShapeLayer *)drawLineOfDashByCAShapeLayer:(UIView *)lineView lineLength:(int)lineLength lineSpacing:(int)lineSpacing lineColor:(UIColor *)lineColor lineDirection:(BOOL)isHorizonal {

    CAShapeLayer *shapeLayer = [CAShapeLayer layer];

    [shapeLayer setBounds:lineView.bounds];

    if (isHorizonal) {

        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame))];

    } else{
        [shapeLayer setPosition:CGPointMake(CGRectGetWidth(lineView.frame) / 2, CGRectGetHeight(lineView.frame)/2)];
    }

    [shapeLayer setFillColor:[UIColor clearColor].CGColor];
    //  设置虚线颜色为blackColor
    [shapeLayer setStrokeColor:lineColor.CGColor];
    //  设置虚线宽度
    if (isHorizonal) {
        [shapeLayer setLineWidth:CGRectGetHeight(lineView.frame)];
    } else {

        [shapeLayer setLineWidth:CGRectGetWidth(lineView.frame)];
    }
    [shapeLayer setLineJoin:kCALineJoinRound];
    //  设置线宽，线间距
    [shapeLayer setLineDashPattern:[NSArray arrayWithObjects:[NSNumber numberWithInt:lineLength], [NSNumber numberWithInt:lineSpacing], nil]];
    //  设置路径
    CGMutablePathRef path = CGPathCreateMutable();
    CGPathMoveToPoint(path, NULL, 0, 0);

    if (isHorizonal) {
        CGPathAddLineToPoint(path, NULL,CGRectGetWidth(lineView.frame), 0);
    } else {
        CGPathAddLineToPoint(path, NULL, 0, CGRectGetHeight(lineView.frame));
    }

    [shapeLayer setPath:path];
    CGPathRelease(path);
    //  把绘制好的虚线添加上来
    [lineView.layer addSublayer:shapeLayer];
    
    return shapeLayer;
}



@end
